/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.framework.base.dao.DaoHelper;
import com.boxin.framework.base.ibatis.PagiParam;
import com.boxin.ims.modules.wechat.entity.MemberLottery;
import com.boxin.ims.modules.wechat.dao.MemberLotteryDao;

/**
 * 用户奖品Service
 * @author XiaoQian
 * @version 2014-05-12
 */
@Component
@Transactional(readOnly = true)
public class MemberLotteryService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(MemberLotteryService.class);
	
	@Autowired
	private MemberLotteryDao memberLotteryDao;
	
	@Autowired
	private DaoHelper daoHelper ;
	
	public MemberLottery get(Long id) {
		return memberLotteryDao.findOne(id);
	}
	
	public Page<Map<String, Object>> find(Page<Map<String, Object>> page, MemberLottery memberLottery) {
		
		
		PagiParam  pagiParam = new PagiParam();
		pagiParam.setCurrentPageNum(page.getPageNo());
		pagiParam.setPageSize(page.getPageSize());
		
		Map<String , Object> params=new HashMap<String, Object>();
		params.put("user_id", memberLottery.getUser().getId());
		List<Map<String, Object>> a = 	daoHelper.queryForPagiList("com.boxin.ims.wechat.statics.queryMemberLotteryByUserId", params, pagiParam);
		
		page.setPageNo(pagiParam.getCurrentPageNum());
		page.setCount(pagiParam.getTotalRowNum());
		page.setPageSize(pagiParam.getPageSize());

		
		return page;
	}
	
//	public List<MemberLottery> find(MemberLottery memberLottery) {
//		DetachedCriteria dc = memberLotteryDao.createDetachedCriteria();
//		ProjectionList projectionList = Projections.projectionList(); 
//		projectionList.add(Property.forName("mobile").count().as("count"));
//		projectionList.add(Property.forName("mobile").group());
//		dc.setProjection(projectionList);
//		dc.addOrder(Order.desc("id"));
//		List<MemberLottery> list= memberLotteryDao.findBySql(page,"SELECT * ,COUNT(id) as count FROM wp_member_lottery where user_id="+memberLottery.getUser().getId()+" GROUP BY mobile,member_name", null);
//		return list;
//	}
	
	@Transactional(readOnly = false)
	public void save(MemberLottery memberLottery) {
		memberLotteryDao.save(memberLottery);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		memberLotteryDao.deleteById(id);
	}
	
}
